@extends('develop.layoutList')

@section('title')
    <li class="cur">
        <span>权限列表</span>
    </li>
@endsection

@section('btn')
    <button class="btn btn-primary" type="button" onclick="Permission.open(0);">
        <span>添加</span>
    </button>
@endsection

@section('search')
    <select class="form-control" name="one_level" id="one_level" onchange="Permission.two_level('one_level','two_level');" ></select>
    <select class="form-control" name="two_level" id="two_level">
        <option value="0">请选择二级权限组</option>
    </select>
    <input class="form-control" style="width:130px;margin-left: 7px;" type="text" name="permissionID"  id="permissionNum" placeholder="请输入权限号">
    <input type="text" style="width:130px;margin-left: 7px;" class="form-control" name="permissionName"  id="permissionDefinition" placeholder="请输入权限名称">
@endsection


@section('js')

    <script>

        var bootstrap_table_ajax_url  = '/lar/develop/permission/search';

        bootstrap_table({
            sortName: 'permissionID',
            sortOrder: 'ASC',
            columns: [
                { title: '操作', field: 'operate', align: 'center' },
                { title: '权限ID', field: 'permissionID', align: 'left'  },
                { title: '权限名', field: 'permissionName', align: 'left'  },
                { title: '权限组名称',  field: 'permissionGroupName', align: 'left' },
                { title: '权限URL', field: 'permissionRequest', align: 'left' }
            ]
        });

        var one_level = JSON.parse('{!! $one_level !!}');
        var two_level = JSON.parse('{!! $two_level !!}');

        var Permission = {

            one_level:function( find ) {

                if ( !E.isEmpty( one_level ) ) {

                    var html = '<option value="">请选择一级权限组</option>';
                    $.each( one_level , function (k,v) {
                        html += '<option value="' + v.group_id + '">' + v.group_name + '</option>';
                    });

                    $('#'+find).html(html);
                }

            },

            two_level:function( find , position) {

                var data = two_level[$('#'+find).val()];

                var html = '<option value="">请选择二级权限组</option>';

                if ( typeof data != 'undefined' && !E.isEmpty( data ) ) {
                    $.each( data , function( k ,v ) {
                        html += '<option value="' + v.group_id + '">' + v.group_name + '</option>';
                    } );
                }

                $('#'+position).html(html);
            },

            open: function( permissionID ) {

                var html = '<form id="edit-form" onsubmit="return false;" class="form-horizontal" style="margin-top: 20px;">';

                html += '<div class="form-group">';
                html += '<label for="permissionGroupID" class="col-sm-3 control-label"><span class="red">*</span> 权限组名：</label>';
                html += '<div class="col-sm-5">';
                html += '<select class="form-control" name="permissionGroupID" id="permissionGroupID" onchange="Permission.two_level(\'permissionGroupID\',\'secondLevelPermissionGroupID\');"></select>';
                html += '</div>';
                html += '</div>';

                html += '<div class="form-group">';
                html += '<label for="secondLevelPermissionGroupID" class="col-sm-3 control-label">&nbsp;</label>';
                html += '<div class="col-sm-5">';
                html += '<select class="form-control" name="secondLevelPermissionGroupID" id="secondLevelPermissionGroupID"><option value="" selected>请选择二级权限组</option></select>';
                html += '</div>';
                html += '</div>';

                html += '<div class="form-group">';
                html += '<label for="permissionID" class="col-sm-3 control-label"><span class="red">*</span> 权限号：</label>';
                html += '<div class="col-sm-5">';
                html += '<input class="form-control" type="text" id="permissionID" name="permissionID" maxlength="10" placeholder="请输入权限号">';
                html += '</div>';
                html += '</div>';

                html += '<div class="form-group">';
                html += '<label for="permissionName" class="col-sm-3 control-label"><span class="red">*</span> 权限名称：</label>';
                html += '<div class="col-sm-5">';
                html += '<input class="form-control" type="text" id="permissionName" name="permissionName" maxlength="30" placeholder="请输入权限名称">';
                html += '</div>';
                html += '</div>';

                html += '<div class="form-group">';
                html += '<label for="permissionRequest" class="col-sm-3 control-label"><span class="red">*</span> 权限路径：</label>';
                html += '<div class="col-sm-9">';
                html += '<input class="form-control" type="text" id="permissionRequest" name="permissionRequest" maxlength="255" placeholder="请输入权限路径">';
                html += '</div>';
                html += '</div>';

                html += '<input type="hidden" name="uuid" id="uuid">';

                html +='</form>';

                layer.open({
                    title: permissionID ? '修改权限' : '新增权限',
                    type: 1,
                    area: '600px',
                    offset: '30px',
                    content: html,
                    shift: 2,
                    scrollbar: false,
                    btn: ['确定', '取消'],
                    yes: function() {
                        Permission.edit();
                    }
                });

                Permission.one_level('permissionGroupID');

                if ( permissionID ) {

                    E.ajax({
                        type: 'get',
                        url: '/lar/develop/permission/get/'+permissionID,
                        success: function (res) {
                            if (res.code == 200) {
                                $('#uuid').val(res.data.uuid);
                                $('#permissionID').val(res.data.permissionID);
                                $('#permissionName').val(res.data.permissionName);
                                $('#permissionRequest').val(res.data.permissionRequest);
                                $('#permissionGroupID').val(res.data.permissionGroupID);
                                $('#permissionGroupID').trigger('onchange');
                                $('#secondLevelPermissionGroupID').val(res.data.secondLevelPermissionGroupID);
                            } else {
                                layer.closeAll();
                                layer.alert( res.message, {icon: 2,offset:'70px'});
                            }
                        }
                    });

                }

                $('#permissionID').focus();

            },

            edit: function() {

                var error_msg = '';
                var dt = E.getFormValues('edit-form');

                if (!E.isInt(dt.permissionID) || dt.permissionID <= 0) {
                    error_msg += '请输入权限号<br>';
                }

                if (E.isEmpty(dt.permissionName)) {
                    error_msg += '请输入权限名称<br>';
                }

                if ( !E.isInt(dt.permissionGroupID) ) {
                    error_msg += '请选择一级权限组<br>';
                }
                if ( !E.isInt(dt.secondLevelPermissionGroupID)) {
                    error_msg += '请选择二级权限组<br>';
                }

                if (E.isEmpty(dt.permissionRequest)) {
                    error_msg += '请输入权限路径<br>';
                }

                if ( error_msg ) {
                    layer.alert( error_msg , {icon:2,offset:'70px'});
                    return false;
                }

                E.ajax({
                    type: 'get',
                    url: '/lar/develop/permission/store',
                    data: dt,
                    success: function ( res ) {

                        if (res.code == 200) {

                            bootstrap_table_init();

                            if ( dt.uuid ) {
                                layer.closeAll();
                            } else {
                                $('#uuid').val('');
                                $('#permissionName').val('');
                                $('#permissionRequest').val('');
                                $('#permissionID').val('').focus();
                            }

                            layer.alert('权限保存成功', {icon: 1, offset:'70px', time: 1500});

                        } else {
                            layer.alert(res.message, {icon: 2,offset:'70px'});
                        }
                    }
                });
            },

            del: function( permissionID ) {

                layer.confirm('您确认删除该权限吗？',{icon: 3,offset:'70px'},function(index) {

                    layer.close(index);

                    E.ajax({
                        type: 'get',
                        url: '/lar/develop/permission/delete/'+permissionID,
                        success: function (res) {
                            if ( res.code == 200 ) {
                                bootstrap_table_init();
                                layer.alert( res.message, {icon:1, offset:'70px', time:1500 } );
                            } else {
                                layer.alert(res.message, {icon: 2 ,offset:'70px'});
                            }
                        }
                    });

                });

            }
        };

        Permission.one_level('one_level');

    </script>

@endsection